#!/bin/bash

##这是强制修改mysql密码的脚本

systemctl stop mysqld
read -p "请输入新密码" pass
expect -c "
spawn /usr/bin/mysql_secure_installation

expect -c "
spawn mysqld_safe --skip-grant-tables &
expect \"mysqld_safe A mysqld process already exists\"
send \"\r\"
expect eof
"
mysql -uroot -e "use mysql;update user set Password=password('$newpass') where user='root';"
echo 是否开启远程访问？[y/n]
read local
if [ $local == y ];then
	mysql -e "GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '$pass' WITH GRANT OPTION;"
else 
	echo 正在配置
fi
mysql -uroot -e "flush privileges;"
pkill mysql
systemctl start mysqld
systemctl enable mysqld